package com.example.dwr.simple;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import org.directwebremoting.io.FileTransfer;

public class UploadDownload
{
  private static final int CHARS_PER_LINE = 21;

  public BufferedImage uploadFiles(BufferedImage uploadImage, String uploadFile, String color)
  {
    uploadImage = scaleToSize(uploadImage);
    uploadImage = grafitiTextOnImage(uploadImage, uploadFile, color);

    return uploadImage;
  }

  public FileTransfer downloadPdfFile(String contents)
    throws Exception
  {
    if ((contents == null) || (contents.length() == 0))
    {
      contents = "[BLANK]";
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter.getInstance(document, buffer);

    document.addCreator("DWR.war using iText");
    document.open();
    document.add(new Paragraph(contents));
    document.close();

    return new FileTransfer("example.pdf", "application/pdf", buffer.toByteArray());
  }

  private BufferedImage scaleToSize(BufferedImage uploadImage)
  {
    AffineTransform atx = new AffineTransform();
    atx.scale(200.0D / uploadImage.getWidth(), 200.0D / uploadImage.getHeight());

    AffineTransformOp afop = new AffineTransformOp(atx, 1);
    uploadImage = afop.filter(uploadImage, null);
    return uploadImage;
  }

  private BufferedImage grafitiTextOnImage(BufferedImage uploadImage, String uploadFile, String color)
  {
    StringBuilder buffer = new StringBuilder();
    while (buffer.length() < 200)
    {
      buffer.append(" ");
      buffer.append(uploadFile);
    }

    Graphics2D g2d = uploadImage.createGraphics();
    for (int row = 0; row < 10; ++row)
    {
      String output = null;
      if (buffer.length() > (row + 1) * 21)
      {
        output = buffer.substring(row * 21, (row + 1) * 21);
      }
      else
      {
        output = buffer.substring(row * 21);
      }

      g2d.setFont(new Font("SansSerif", 1, 16));
      g2d.setColor(decodeHtmlColorString(color));
      g2d.drawString(output, 5, (row + 1) * 21);
    }

    return uploadImage;
  }

  public static Color decodeHtmlColorString(String colorString)
  {
    Color color;
    int red;
    int green;
    int blue;
    if (colorString.startsWith("#"))
    {
      colorString = colorString.substring(1);
    }
    if (colorString.endsWith(";"))
    {
      colorString = colorString.substring(0, colorString.length() - 1);
    }

    switch (colorString.length())
    {
    case 6:
      red = Integer.parseInt(colorString.substring(0, 2), 16);
      green = Integer.parseInt(colorString.substring(2, 4), 16);
      blue = Integer.parseInt(colorString.substring(4, 6), 16);
      color = new Color(red, green, blue);
      break;
    case 3:
      red = Integer.parseInt(colorString.substring(0, 1), 16);
      green = Integer.parseInt(colorString.substring(1, 2), 16);
      blue = Integer.parseInt(colorString.substring(2, 3), 16);
      color = new Color(red, green, blue);
      break;
    case 1:
      red = green = blue = Integer.parseInt(colorString.substring(0, 1), 16);
      color = new Color(red, green, blue);
      break;
    default:
      throw new IllegalArgumentException("Invalid color: " + colorString);
    }
    return color;
  }
}